<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <title>区域商家管理</title>
    <!-- Le styles -->
    <link href="/common/js/bootstrap-3.0.0/css/bootstrap-united.min.css" rel="stylesheet">
    <link href="/common/js/artDialog/skins/default.css" rel="stylesheet">
    <style type="text/css">
        body {
            padding: 10px 0;
        }
    </style>
</head>
<body>
<div class="container">

    <form id="form" method="post" class="well form-horizontal">
        <fieldset>
            <legend>用户管理</legend>
            <div class="form-group">
                <div class="col-md-6">
                    <label class="col-md-2 control-label" for="cn_name">昵称</label>

                    <div class="col-md-10">
                        <input id="cn_name" name="cn_name" type="text" class="form-control"/>
                    </div>
                </div>

                <div class="col-md-6">
                    <label class="col-md-2 control-label" for="userName">手机号</label>

                    <div class="col-md-10">
                        <input id="userName" name="username" type="text" class="form-control"/>
                    </div>
                </div>

                <div class="col-md-6">
                    <label class="col-md-2 control-label" for="userName">身份证</label>

                    <div class="col-md-10">
                        <input id="identity" name="identity" type="text" class="form-control"/>
                    </div>
                </div>

            <div class="form-group">
                <div class="col-md-12 text-right">
                    <a style="width: 80px;" href="/user/addUserPage" class="btn btn-success">添加用户</a>
                    <button type="button" id="searchBtn" style="width: 80px;" class="btn btn-primary">查询</button>
                </div>
            </div>
        </fieldset>
    </form>

    <table id="resultTable" class="table table-bordered table-hover">
        <thead>
        <tr>
            <th>ID</th>
            <th>姓名</th>
            <th>类型</th>
            <th>邮箱</th>
            <#--<th>角色</th>-->
            <th>省份</th>
            <th>城市</th>
            <th>区域</th>
            <th>注册时间</th>
            <th>状态</th>
            <#--<th>描述</th>-->
        </tr>
        </thead>
        <tbody>
        <script type="text/html">
            <tr>
                <td>{{id}}</td>
                <td>{{cnName!''}}</td>
                <td>{{userType?renderUserType}}</td>
                <td>{{email!''}}</td>
                <td>{{province!''}}</td>
                <td>{{city!''}}</td>
                <td>{{area!''}}</td>
                <td>{{createTime?renderDateStr}}</td>
                <#--<td>{{roleName!''}}</td>-->
                <td>{{status?renderStatus}}</td>
                <#--<td>{{userDescn!''}}</td>-->
            </tr>
        </script>
        </tbody>
    </table>
</div>

<script type="text/javascript" src="/common/js/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="/common/js/bootstrap-table.20130913.js"></script>
<script type="text/javascript" src="/common/js/artDialog/jquery.artDialog.min.js"></script>
<script type="text/javascript" src="/common/js/artDialog/artDialog.plugins.min.js"></script>
<script type="text/javascript">
    $(function () {
        (function (d) {
            d['okValue'] = '确定';
            d['cancelValue'] = '取消';
            d['title'] = '消息';
            // [more..]
        })($.dialog.defaults);

        var param = $("#form").serialize();
        var s = $("#resultTable").bsTable({
            url: '/agent/viewUsers.json',
            ajaxType: "POST",  //ajax 提交方式 post 或者 get
            pageNo: 1,
            pageNoAlias: 'page',
            pageSize: 10,
            pagingAlign: "right",
            param: param,
            countRoot: "total",
            dataRoot: "list"
        });

        $("#searchBtn").on('click', function () {
            var param = $("#form").serialize();
            s.reload(param);
        });
    });

    function search() {
        $("#searchBtn").click();
    }

    function renderDateStr(createTime) {
        var time = new Date(createTime);
        var y = time.getFullYear();//年
        var m = time.getMonth() + 1;//月
        var d = time.getDate();//日
        var h = time.getHours();//时
        var mm = time.getMinutes();//分
        var s = time.getSeconds();//秒
        return y+"-"+m+"-"+d+" "+h+":"+mm+":"+s;
    }

    function renderStatus(status) {
        if (status == 1) {
            return "启用";
        } else {
            return "冻结";
        }
    }

    function renderUserType(userType) {
        if (userType == 1) {
            return "超级管理员";
        }else if (userType == 5) {
            return "区域加盟商";
        }else if (userType == 4) {
            return "团队商家";
        }else if (userType == 3) {
            return "普通商家";
        } else {
            return "普通用户";
        }
    }

    function getOperate(row) {
        if (row) {
            var html = "<a href='/user/editUser?id=" + row.userId + "'>编辑</a>";
//            if (row.userType == 2) {
                html += "&nbsp;&nbsp;<a href='javascript:void(0);' onclick='resetPassword(" + row.userId + ")'>重置密码</a>";
                if (row.status == 1) {
                    html += "&nbsp;&nbsp;<a href='javascript:void(0);' onclick='setStatus(" + row.userId + ")'>禁用</a>";
                } else {
                    html += "&nbsp;&nbsp;<a href='javascript:void(0);' onclick='setStatus(" + row.userId + ")'>启用</a>";
                }
//            }

            if (row.userType == 3 || row.userType == 4){
                html += "&nbsp;&nbsp;<a href='/user/addDealCountView?user_id=" + row.userId + "&biz_deal_count="+row.bizDealCount+"'>修改订单数</a>";
            }
            return html;
        } else {
            return "";
        }
    }

    function resetPassword(userId) {
        $.confirm("是否重置密码？", function () {
            $.ajax({
                url: "/user/resetPassword.json",
                type: "post",
                data: {id: userId},
                success: function (data) {
                    if (data.msg == "FAIL") {
                        $.alert("重置失败！");
                    } else if (data.msg == "LDAP") {
                        $.alert("LDAP用户不能重置密码！")
                    } else {
                        $.alert("新密码:" + data.msg);
                    }
                }
            });
        });
    }

    function setStatus(userId) {
        $.ajax({
            url: "/user/setStatus.json",
            type: "post",
            data: {id: userId},
            success: function (data) {
                if (data.msg == "USER_NOT_EXIST") {
                    $.alert("用户不存在！");
                } else if (data.msg == "FAIL") {
                    $.alert("操作失败！");
                } else {
//                    window.location.reload();
                    $.alert("操作成功！");
                    search();
                }
            }
        });
    }
</script>
</body>
</html>